<!DOCTYPE html>
<html lang="zh_cn">
<head>
          <title>来玩魔王的咚</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta charset="utf-8" />
        <!-- twitter card metadata -->
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="/images/mowang.png">
<meta name="twitter:site" content="">
<meta name="twitter:title" content="goroutine 和通道">
<meta name="twitter:description" content="<p>并发编程风格；通道：无缓冲通道，管道，单向通道，缓冲通道，并发请求镜像资源，goroutine泄露；select多路复用：非阻塞模式，通道的零值；示例并发目录遍历：递归遍历目录，计算大小，汇报进度，提高并发效率，限制并发；取消（广播）：发送广播，响应取消</p>">
        <!-- OG Tags -->
<meta property="og:url" content="/gopl-rotoutine-chennle.html"/>
<meta property="og:title" content="来玩魔王的咚 | goroutine 和通道" />
<meta property="og:description" content="<p>并发编程风格；通道：无缓冲通道，管道，单向通道，缓冲通道，并发请求镜像资源，goroutine泄露；select多路复用：非阻塞模式，通道的零值；示例并发目录遍历：递归遍历目录，计算大小，汇报进度，提高并发效率，限制并发；取消（广播）：发送广播，响应取消</p>" />
        <!-- favicon -->
        <link rel="icon" type="image/png" href="/images/mowang.png">
        <!-- moment.js for date formatting -->
        <script src="/theme/js/moment.js"></script>
        <!-- css -->
        <link rel="stylesheet" type="text/css" href="/theme/css/main.css" />
        <!-- 左边的menu，如果页面高度不够，就跟着滚动，否则文章分类显示不全 -->
        <link rel="stylesheet" type="text/css" href="/theme/css/mycss/menu.css" />
		<script>
			
                /*! grunt-grunticon Stylesheet Loader - v2.1.2 | https://github.com/filamentgroup/grunticon | (c) 2015 Scott Jehl, Filament Group, Inc. | MIT license. */
    
    (function(e){function t(t,n,r,o){"use strict";function a(){for(var e,n=0;u.length>n;n++)u[n].href&&u[n].href.indexOf(t)>-1&&(e=!0);e?i.media=r||"all":setTimeout(a)}var i=e.document.createElement("link"),l=n||e.document.getElementsByTagName("script")[0],u=e.document.styleSheets;return i.rel="stylesheet",i.href=t,i.media="only x",i.onload=o||null,l.parentNode.insertBefore(i,l),a(),i}var n=function(r,o){"use strict";if(r&&3===r.length){var a=e.navigator,i=e.Image,l=!(!document.createElementNS||!document.createElementNS("http://www.w3.org/2000/svg","svg").createSVGRect||!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")||e.opera&&-1===a.userAgent.indexOf("Chrome")||-1!==a.userAgent.indexOf("Series40")),u=new i;u.onerror=function(){n.method="png",n.href=r[2],t(r[2])},u.onload=function(){var e=1===u.width&&1===u.height,a=r[e&&l?0:e?1:2];n.method=e&&l?"svg":e?"datapng":"png",n.href=a,t(a,null,null,o)},u.src="",document.documentElement.className+=" grunticon"}};n.loadCSS=t,e.grunticon=n})(this);(function(e,t){"use strict";var n=t.document,r="grunticon:",o=function(e){if(n.attachEvent?"complete"===n.readyState:"loading"!==n.readyState)e();else{var t=!1;n.addEventListener("readystatechange",function(){t||(t=!0,e())},!1)}},a=function(e){return t.document.querySelector('link[href$="'+e+'"]')},c=function(e){var t,n,o,a,c,i,u={};if(t=e.sheet,!t)return u;n=t.cssRules?t.cssRules:t.rules;for(var l=0;n.length>l;l++)o=n[l].cssText,a=r+n[l].selectorText,c=o.split(");")[0].match(/US\-ASCII\,([^"']+)/),c&&c[1]&&(i=decodeURIComponent(c[1]),u[a]=i);return u},i=function(e){var t,o,a;o="data-grunticon-embed";for(var c in e)if(a=c.slice(r.length),t=n.querySelectorAll(a+"["+o+"]"),t.length)for(var i=0;t.length>i;i++)t[i].innerHTML=e[c],t[i].style.backgroundImage="none",t[i].removeAttribute(o);return t},u=function(t){"svg"===e.method&&o(function(){i(c(a(e.href))),"function"==typeof t&&t()})};e.embedIcons=i,e.getCSS=a,e.getIcons=c,e.ready=o,e.svgLoadedCallback=u,e.embedSVG=u})(grunticon,this);
                
                grunticon(["/theme/css/icons.data.svg.css", "/theme/css/icons.data.png.css", "/theme/css/icons.fallback.css"]);
            </script>
        <noscript><link href="/theme/css/icons.fallback.css" rel="stylesheet"></noscript>
        <!-- menu toggle javascript -->
        <script type="text/javascript">
            document.addEventListener("DOMContentLoaded", initMenu);
            
            function initMenu(){
                var menu = document.getElementById("menu");
                var menulink = document.getElementById("menu-link");
                menulink.addEventListener("click", function toggleMenu(){
                        window.event.preventDefault();
                        menulink.classList.toggle('active');
                        menu.classList.toggle('active');              
                    });
            };
        </script>
        <!-- 不蒜子 -->
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <meta name="description" content="<p>并发编程风格；通道：无缓冲通道，管道，单向通道，缓冲通道，并发请求镜像资源，goroutine泄露；select多路复用：非阻塞模式，通道的零值；示例并发目录遍历：递归遍历目录，计算大小，汇报进度，提高并发效率，限制并发；取消（广播）：发送广播，响应取消</p>" />

    <meta name="tags" content="gopl" />
  <!-- 替换部分base的样式，看文章时，要再宽一点，右边有很多空间可以撑开 -->
  <link rel="stylesheet" type="text/css" href="/theme/css/mycss/article.css" />

</head>
<body>
    <div role="banner" id="masthead">
        <header>
            <a href="/"><img src="/images/mowang.png" alt="McManus Logo"></a>
                <h1>来玩魔王的咚@骑士救兵</h1>
            <a href="#menu" id="menu-link">more stuff</a>
            <nav id="menu">
                <ul>
                        <li><a href="/tags">tags</a></li>
                            <li><a href="/category/cloud.html">Cloud</a></li>
                            <li><a href="/category/docker.html">Docker</a></li>
                            <li class="active"><a href="/category/go.html">Go</a></li>
                            <li><a href="/category/linux.html">Linux</a></li>
                            <li><a href="/category/python.html">Python</a></li>
                            <li><a href="/category/xue-xi-bi-ji.html">学习笔记</a></li>
                            <li><a href="/category/yun-wei-zi-dong-hua.html">运维自动化</a></li>
                </ul>
            </nav>
        </header>
    </div>
        <div class="page" role="main">
  <div class="article" role="article">
    <article>
        <footer>
            <a name="top"></a>
            <p>
              <time datetime=" 2020-08-05 17:10:00+08:00">
                <script>document.write(moment('2020-08-05 17:10:00+08:00').format('LL'));</script>
              </time>
              ~
              <time datetime=" 2020-08-05 17:10:00+08:00">
                <script>document.write(moment('2020-08-05 17:10:00+08:00').format('LL'));</script>
              </time>
            </p>
        </footer>
        <header>
          <h2>
            goroutine 和通道
          </h2>
        </header>
      <div class="content">
         <div class="toc">
<ul>
<li><a href="#go-yu-yan-de-bing-fa-bian-cheng-feng-ge">Go 语言的并发编程风格</a></li>
<li><a href="#tong-dao">通道</a><ul>
<li><a href="#wu-huan-chong-tong-dao">无缓冲通道</a></li>
<li><a href="#guan-dao">管道</a></li>
<li><a href="#dan-xiang-tong-dao-lei-xing">单向通道类型</a></li>
<li><a href="#huan-chong-tong-dao">缓冲通道</a></li>
<li><a href="#tong-dao-huan-chong-de-xuan-ze">通道缓冲的选择</a></li>
</ul>
</li>
<li><a href="#shi-yong-select-duo-lu-fu-yong">使用 select 多路复用</a><ul>
<li><a href="#shi-yong-shi-li">使用示例</a></li>
<li><a href="#fei-zu-se-mo-shi">非阻塞模式</a></li>
<li><a href="#tong-dao-de-ling-zhi">通道的零值</a></li>
</ul>
</li>
<li><a href="#shi-li-bing-fa-mu-lu-bian-li">示例：并发目录遍历</a><ul>
<li><a href="#di-gui-bian-li-mu-lu">递归遍历目录</a></li>
<li><a href="#ji-suan-da-xiao-bing-shu-chu">计算大小并输出</a></li>
<li><a href="#hui-bao-jin-du">汇报进度</a></li>
<li><a href="#ti-gao-bing-fa-xiao-lv">提高并发效率</a></li>
<li><a href="#xian-zhi-bing-fa">限制并发</a></li>
</ul>
</li>
<li><a href="#qu-xiao-guang-bo">取消（广播）</a><ul>
<li><a href="#cha-xun-zhuang-tai">查询状态</a></li>
<li><a href="#fa-song-qu-xiao-guang-bo">发送取消广播</a></li>
<li><a href="#xiang-ying-qu-xiao-cao-zuo">响应取消操作</a></li>
<li><a href="#ce-shi-de-ji-qiao">测试的技巧</a></li>
</ul>
</li>
</ul>
</div>
<h3 id="go-yu-yan-de-bing-fa-bian-cheng-feng-ge"><a class="toclink" href="#go-yu-yan-de-bing-fa-bian-cheng-feng-ge">Go 语言的并发编程风格</a></h3>
<p>Go 有两种并发编程风格：</p>
<ul>
<li>goroutine 和通道（chennle），支持<strong>通信顺序进程</strong>（Communicating Sequential Process, CSP），CSP 是一个并发的模式，在不同的执行体（goroutine）之间传递值，但是变量本身局限于单一的执行体。</li>
<li><strong>共享内存多线程</strong>的传统模型，这和在其他主流语言中使用的线程类似。</li>
</ul>
<p>这章讲第一种 goroutine 和通道。  </p>
<h3 id="tong-dao"><a class="toclink" href="#tong-dao">通道</a></h3>
<p>如果说 goroutine 是 Go 程序并发的执行体，<strong>通道</strong>就是它们之间的连接。每一个通道是一个具体类型的导管，叫作通道的<strong>元素类型</strong>。<br>
像 map 一样，通道是一个使用 make 创建的数据结构的应用。当复制或者作为参数传递到一个函数时，复制的是引用，这样调用者和被调用者都引用了同一份数据结构。和其他引用类型一样，通道的零值是 nil。<br>
通道有两个主要操作：<strong>发送</strong>（send）和<strong>接收</strong>（receive），两者统称为<strong>通信</strong>。通道还支持第三个操作：<strong>关闭</strong>（close），它设置一个标志位来指示值当前已经发送完毕。<br>
使用简单的 make 调用创建的通道叫<strong>无缓冲（unbuffered）通道</strong>，但 make 还可以接受第二个可选参数，一个表示通道<strong>容量</strong>的整数。如果容量是0，创建的也是无缓冲通道。  </p>
<h4 id="wu-huan-chong-tong-dao"><a class="toclink" href="#wu-huan-chong-tong-dao">无缓冲通道</a></h4>
<p>使用无缓冲通道进行的通信导致发送和接收 goroutine <strong>同步化</strong>。因此无缓冲通道也称为<strong>同步通道</strong>。<br>
通过通道发送消息有两个重要的方面需要考虑：</p>
<ul>
<li>每条消息有一个值</li>
<li>通信本身以及通信发生的时间。当我们强调这方面的时候，把消息叫做<strong>事件</strong>（event）</li>
</ul>
<p>当事件没有携带额外的信息时，它单纯的目的是进行同步。和 map 实现的集合一样，可以使用一个 struct{} 元素类型的通道来强调它，尽管通常使用 bool 或 int 类型的通道来做相同的事情。因为<code>done &lt;- 1</code>更简短。<em>书上讲集合的时候，使用的是 bool类型，这里讲事件同步，使用的是空结构体。</em>  </p>
<h4 id="guan-dao"><a class="toclink" href="#guan-dao">管道</a></h4>
<p>通道可以用来连接 goroutine，这样一个的输出是另一个的输入，这个叫<strong>管道</strong>（pipline）。  </p>
<h5 id="guan-bi-tong-dao"><a class="toclink" href="#guan-bi-tong-dao">关闭通道</a></h5>
<p>如果发送方知道没有更多的数据要发送，告诉接收者所在的 goroutine 可以停止等待是很有用的。<br>
这可以通过调用内置的 Close 函数来关闭通道：</p>
<div class="highlight"><pre><span></span><code><span class="nx">ch1</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kt">bool</span><span class="p">)</span><span class="w"> </span><span class="c1">// 创建通道 ch1</span><span class="w"></span>
<span class="c1">// 下面是关闭通道</span><span class="w"></span>
<span class="nb">close</span><span class="p">(</span><span class="nx">ch1</span><span class="p">)</span><span class="w"></span>
</code></pre></div>

<p>在通道关闭后，任何后续的发送操作将会导致应用崩溃。当关闭的通道被<strong>读完</strong>（就是最后一个发送的值被接收）后，所有后续的接收操作都会立即返回，返回值是对应类型的零值。<br>
关闭通道还可以作为一个广播机制，后面的章节会具体讲。  </p>
<h5 id="jian-cha-tong-dao-de-guan-bi"><a class="toclink" href="#jian-cha-tong-dao-de-guan-bi">检查通道的关闭</a></h5>
<p>没有一个直接的方式来判断是否通道已经关闭，不过可以接收返回两个参数：接收到的元素，以及一个布尔值（通常是ok），返回 true 表示接收成功，返回 false 表示当前的接收操作在一个关闭的并且读完的通道上。<em>这个方法检查的也不是通道是否关闭了，而是通道里的值是否已经取完了。只有关闭的通道，才能保证不会有新值进入，把里面的值都取完后，会返回 false 表示这次取到的是通道关闭后的零值，而不是原本就是一个值为零的数据。</em>  </p>
<p>另外，还提供了一个 range 循环语法可以在通道上迭代。这个语法更为方便接收在通道上所有发送的值，接收完最后一个值后结束循环。  </p>
<h5 id="la-ji-hui-shou"><a class="toclink" href="#la-ji-hui-shou">垃圾回收</a></h5>
<p>结束时，关闭没一个通道不是必需的。只有在通知接收方 goroutine 所有的数据都发送完毕的时候才需要关闭通道。通道也可以通过垃圾回收器根据它是否可以访问来决定是否回收它，而不是根据它是否关闭。  </p>
<h4 id="dan-xiang-tong-dao-lei-xing"><a class="toclink" href="#dan-xiang-tong-dao-lei-xing">单向通道类型</a></h4>
<p>Go 还提供了<strong>单向通道</strong>类型，仅仅导出发送或接收操作。类型<code>chan&lt;- int</code>是一个<strong>只能发送</strong>的通道，允许发送单不允许接收。反之，类型<code>&lt;-chan int</code>是一个<strong>只能接收</strong>的通道，允许接收但是不能发送。这里像箭头一样的操作符相对于 chan 关键字的位置是一个帮助记忆的点。如果违反这里的接收或发送的原则，在编译时会被检查出来。<br>
在函数定义时，指定了单向通道的类型。在函数调用时，依然是把正常定义的双向通道类型传值给函数的参数。函数的调用会隐式地将普通的通道类型转化为要求的单向通道的类型。在任何赋值操作中将双向通道转换为单向通道都是允许的，但是反过来是不行的。一旦有一个单向通道，是没有办法通过它获取到引用同一个数据结构的双向通道的类型的。  </p>
<h4 id="huan-chong-tong-dao"><a class="toclink" href="#huan-chong-tong-dao">缓冲通道</a></h4>
<p>缓冲通道有一个元素队列，队列的最大长度在创建的时候通过 make 的容量参数来设置：</p>
<div class="highlight"><pre><span></span><code><span class="nx">ch1</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kt">string</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"></span>
</code></pre></div>

<p>通过调用内置的 cap 函数，可以获取通道缓冲区的容量。这种需求不太常见。<br>
通过调用内置的 len 函数，可以获取通道内的元素个数。不过在并发程序中这个信息会随着检索操作很快过时，所以它的价值很低，但是它在错误诊断和性能优化的时候很有用。  </p>
<h5 id="zhe-bu-shi-dui-lie"><a class="toclink" href="#zhe-bu-shi-dui-lie">这不是队列</a></h5>
<p>发送和接收操作可以在同一个 goroutine 中，但在真实的程序中通常由不同的 goroutine 执行。因为语法简单，新手有时候粗暴地将缓冲通道作为队列在单个 goroutine 中使用，但是这是个错误的用法。通道和 goroutine 的调度深度关联，如果没有另一个 goroutine 从通道进行接收，发送者（也许是整个程序）有被永久阻塞的风险。如果仅仅需要一个简单的队列，使用切片创建一个就好了。  </p>
<h5 id="shi-li-bing-fa-qing-qiu-zui-kuai-de-jing-xiang-zi-yuan"><a class="toclink" href="#shi-li-bing-fa-qing-qiu-zui-kuai-de-jing-xiang-zi-yuan">示例：并发请求最快的镜像资源</a></h5>
<p>下面的例子展示一个使用缓冲通道的应用。它并发地向三个<strong>镜像地址</strong>发请求，镜像指相同但分布在不同地理区域的服务器。它将它们的响应通过一个缓冲通道进行发送，然后只接收第一个返回的响应，因为它是最早到达的。所以 mirroredQuery 函数甚至在两个比较慢的服务器还没有响应之前返回了一个结果。（偶然情况下，会出现像这个例子中的几个 goroutine 同时在一个通道上并发发送，或者同时从一个通道接收的情况。）：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">mirroredQuery</span><span class="p">()</span><span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">responses</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kt">string</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="c1">// 有几个镜像，就要多大的容量，不能少</span><span class="w"></span>
<span class="w">    </span><span class="k">go</span><span class="w"> </span><span class="kd">func</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">responses</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">request</span><span class="p">(</span><span class="s">&quot;asia.gopl.io&quot;</span><span class="p">)</span><span class="w"> </span><span class="p">}()</span><span class="w"></span>
<span class="w">    </span><span class="k">go</span><span class="w"> </span><span class="kd">func</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">responses</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">request</span><span class="p">(</span><span class="s">&quot;europe.gopl.io&quot;</span><span class="p">)</span><span class="w"> </span><span class="p">}()</span><span class="w"></span>
<span class="w">    </span><span class="k">go</span><span class="w"> </span><span class="kd">func</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">responses</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">request</span><span class="p">(</span><span class="s">&quot;americas.gopl.io&quot;</span><span class="p">)</span><span class="w"> </span><span class="p">}()</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">responses</span><span class="w"> </span><span class="c1">// 返回最快一个获取到的请求结果</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">request</span><span class="p">(</span><span class="nx">hostname</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nx">response</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">&quot;省略获取返回的代码&quot;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>

<h5 id="goroutine-xie-lou"><a class="toclink" href="#goroutine-xie-lou">goroutine 泄露</a></h5>
<p>在上面的示例中，如果使用的是无缓冲通道，两个比较慢的 goroutine 将被卡住，因为在它们发送响应结果到通道的时候没有 goroutine 来接收。这个情况叫做 <strong>goroutine 泄漏</strong>。它属于一个 bug。不像回收变量，泄漏的 goroutine 不会自动回收，所以要确保 goroutine 在不再需要的时候可以自动结束。  </p>
<h4 id="tong-dao-huan-chong-de-xuan-ze"><a class="toclink" href="#tong-dao-huan-chong-de-xuan-ze">通道缓冲的选择</a></h4>
<p>无缓冲和缓冲通道的选择，缓冲通道容量大小的选择，都会对程序的正确性产生影响。无缓冲通道提供强同步保障，因为每一次发送都需要和一次对应的接收同步；对于缓冲通道，这些操作则是解耦的。如果知道要发送的值数量的上限，通常会创建一个容量是使用上限的缓冲通道，在接收第一个值前就完成所有的发送。在内存无法提供缓冲容量的情况下，可能导致程序死锁。  </p>
<h3 id="shi-yong-select-duo-lu-fu-yong"><a class="toclink" href="#shi-yong-select-duo-lu-fu-yong">使用 select 多路复用</a></h3>
<p>有时候需要在多个通道上接收，不能只从一个通道上接收，因为任何一个操作都会在完成前阻塞。<br>
所以需要<strong>多路复用</strong>那些操作过程，为了实现这个目的，需要一个 select 语句：</p>
<div class="highlight"><pre><span></span><code><span class="k">select</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="k">case</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">ch1</span><span class="p">:</span><span class="w"></span>
<span class="w">    </span><span class="c1">// ...</span><span class="w"></span>
<span class="k">case</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">ch2</span><span class="p">:</span><span class="w"></span>
<span class="w">    </span><span class="c1">// ...use x...</span><span class="w"></span>
<span class="k">case</span><span class="w"> </span><span class="nx">ch3</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">y</span><span class="p">:</span><span class="w"></span>
<span class="w">    </span><span class="c1">// ...</span><span class="w"></span>
<span class="k">default</span><span class="p">:</span><span class="w"></span>
<span class="w">    </span><span class="c1">// ...</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>上面展示的是 select 语句的通用形式。像 switch 语句一样，它有一系列的情况和一个可选的默认分支。每一个情况指定一次<strong>通信</strong>（在一些通道上进行发送或接收操作）和关联的一段代码块。接收表达式操作可能出现在它本身上，像第一个情况，或者在一个短变量声明中，像第二个情况。第二种形式可以让你引用所接收的值。<br>
select 一直等待，直到一次通信来告知有一些情况可以执行。然后，它进行这次通信，执行此情况所对应的语句，其他的通信将不会发生。  </p>
<h4 id="shi-yong-shi-li"><a class="toclink" href="#shi-yong-shi-li">使用示例</a></h4>
<p>下面是一个微妙的例子。通道 ch 的缓冲区大小为 1，它要么是空的，要么是满的，因此只有在其中一个状况下可以执行，要么在 i 是偶数时发送，要么在 i 是奇数时接收。<br>
它总是输出 0 2 4 6 8：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">ch</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">select</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">ch</span><span class="p">:</span><span class="w"></span>
<span class="w">            </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">x</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="nx">ch</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">i</span><span class="p">:</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>如果多个情况同时满足，select 随机选择一个，这样保证每一个通道有相同的机会被选中。在前一个例子中增加缓冲区的容量，会使输出变得不可确定，因为当缓冲既不空也不满的情况，相当于 select 语句在随机做选择。  </p>
<h4 id="fei-zu-se-mo-shi"><a class="toclink" href="#fei-zu-se-mo-shi">非阻塞模式</a></h4>
<p>有时候我们试图在一个通道上发送或接收，但是不想在通道没有准备好的情况下被阻塞，<strong>非阻塞通信</strong>。这使用 select 语句也可以做到。select 可以有一个默认情况，它用来指定在没有其他的通信发生时可以立即执行的动作。<br>
下面的 select 语句尝试从 abort 通道中接收一个值，如果没有值，它什么也不做。这是一个非阻塞的接收操作。<br>
重复这个动作称为对通道<strong>轮询</strong>：</p>
<div class="highlight"><pre><span></span><code><span class="k">select</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="k">case</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">abort</span><span class="p">:</span><span class="w"></span>
<span class="w">    </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;Launch aborted!&quot;</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"></span>
<span class="k">default</span><span class="p">:</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 不执行任何操作</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<h4 id="tong-dao-de-ling-zhi"><a class="toclink" href="#tong-dao-de-ling-zhi">通道的零值</a></h4>
<p>通道的零值是 nil。令人惊讶的是，nil 通道有时候很有用。因为在 nil 通道上发送和接收将永远阻塞。对于 select 语句中的情况，如果其通道是 nil，它将永远不会被选择。可以用 nil 来开启或禁用特性所对应的情况，比如超时处理或者取消操作，响应其他的输入事件或者发送事件。  </p>
<h3 id="shi-li-bing-fa-mu-lu-bian-li"><a class="toclink" href="#shi-li-bing-fa-mu-lu-bian-li">示例：并发目录遍历</a></h3>
<p>最后来一个示例的实战。这里要构建一个程序，根据命令行指定的输入，报告一个或多个目录的磁盘使用情况，类似 UNIX 的 du 命令。  </p>
<h4 id="di-gui-bian-li-mu-lu"><a class="toclink" href="#di-gui-bian-li-mu-lu">递归遍历目录</a></h4>
<p>大多数的工作由下面的 walkDir 函数完成，它使用 dirents 辅助函数来枚举目录中的条目：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// walkDir 递归地遍历以 dir 为根目录的整个文件树</span><span class="w"></span>
<span class="c1">// 并在 fileSizes 上发送每个已找到的文件的大小</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="nx">walkDir</span><span class="p">(</span><span class="nx">dir</span><span class="w"> </span><span class="kt">string</span><span class="p">,</span><span class="w"> </span><span class="nx">fileSizes</span><span class="w"> </span><span class="kd">chan</span><span class="o">&lt;-</span><span class="w"> </span><span class="kt">int64</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">entry</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">dirents</span><span class="p">(</span><span class="nx">dir</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">IsDir</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">subdir</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">filepath</span><span class="p">.</span><span class="nx">Join</span><span class="p">(</span><span class="nx">dir</span><span class="p">,</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">Name</span><span class="p">())</span><span class="w"></span>
<span class="w">            </span><span class="nx">walkDir</span><span class="p">(</span><span class="nx">subdir</span><span class="p">,</span><span class="w"> </span><span class="nx">fileSizes</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">fileSizes</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">Size</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// dirents 返回 dir 目录中的条目</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="nx">dirents</span><span class="p">(</span><span class="nx">dir</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">[]</span><span class="nx">os</span><span class="p">.</span><span class="nx">FileInfo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">entries</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">ioutil</span><span class="p">.</span><span class="nx">ReadDir</span><span class="p">(</span><span class="nx">dir</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Fprintf</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stderr</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;du1: %v\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">entries</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>ioutil.ReadDir 函数返回一个 os.FileInfo 类型的切片，针对单个文件同样的信息可以通过调用 os.Stat 函数来返回。对每一个子目录，walkDir 递归调用它自己，对于每一个文件，walkDir 发送一条消息到 fileSizes 通道。消息是文件所占用的字节数。  </p>
<h4 id="ji-suan-da-xiao-bing-shu-chu"><a class="toclink" href="#ji-suan-da-xiao-bing-shu-chu">计算大小并输出</a></h4>
<p>下面的 main 函数使用两个 goroutine。后台 goroutine 调用 walkDir 遍历命令行上指定的每一个目录，最后关闭 fileSizes 通道。<br>
主 goroutine 计算从通道中接收的文件的大小的和，最后输出总数：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 确定初始目录</span><span class="w"></span>
<span class="w">    </span><span class="nx">flag</span><span class="p">.</span><span class="nx">Parse</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="nx">roots</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">flag</span><span class="p">.</span><span class="nx">Args</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">roots</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">roots</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[]</span><span class="kt">string</span><span class="p">{</span><span class="s">&quot;.&quot;</span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 遍历文件树</span><span class="w"></span>
<span class="w">    </span><span class="nx">fileSizes</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kt">int64</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">go</span><span class="w"> </span><span class="kd">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">root</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">roots</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">walkDir</span><span class="p">(</span><span class="nx">root</span><span class="p">,</span><span class="w"> </span><span class="nx">fileSizes</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="nb">close</span><span class="p">(</span><span class="nx">fileSizes</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}()</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 输出结果</span><span class="w"></span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="w"> </span><span class="kt">int64</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">size</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">fileSizes</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">nfiles</span><span class="o">++</span><span class="w"></span>
<span class="w">        </span><span class="nx">nbytes</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nx">size</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="nx">printDiskUsage</span><span class="p">(</span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">printDiskUsage</span><span class="p">(</span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="w"> </span><span class="kt">int64</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;%d files   %.2f GB\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nb">float64</span><span class="p">(</span><span class="nx">nbytes</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">30</span><span class="p">))</span><span class="w"> </span><span class="c1">// 1&lt;&lt;30 就是 2**30 就是 1024*1024*1024</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>现在程序可以正常的工作。  </p>
<h4 id="hui-bao-jin-du"><a class="toclink" href="#hui-bao-jin-du">汇报进度</a></h4>
<p>如果程序可以汇报进度的话，会更加友好。如果仅仅只是把 printDiskUsage 调用移动到循环内部，会有非常多的输出。<br>
下面的示例，修改了主 goroutine 中记录结果的部分。不是在每次迭代中输出，而是加了一个定时器，通过 select 定期输出一次结果。另外还加上了 -v 参数来控制，可以选择性的开启这个功能。如果不开启功能，那么 tick 通道的值就是 nil，它对应的分支在select 中就永远是阻塞的。<br>
相当于没有开启这个选项，很直观的理解：</p>
<div class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">verbose</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">flag</span><span class="p">.</span><span class="nx">Bool</span><span class="p">(</span><span class="s">&quot;v&quot;</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;周期性的输出进度&quot;</span><span class="p">)</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 确定初始目录，没变化</span><span class="w"></span>
<span class="w">    </span><span class="nx">flag</span><span class="p">.</span><span class="nx">Parse</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="nx">roots</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">flag</span><span class="p">.</span><span class="nx">Args</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">roots</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">roots</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[]</span><span class="kt">string</span><span class="p">{</span><span class="s">&quot;.&quot;</span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 遍历文件树，没变化</span><span class="w"></span>
<span class="w">    </span><span class="nx">fileSizes</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kt">int64</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">go</span><span class="w"> </span><span class="kd">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">root</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">roots</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">walkDir</span><span class="p">(</span><span class="nx">root</span><span class="p">,</span><span class="w"> </span><span class="nx">fileSizes</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="nb">close</span><span class="p">(</span><span class="nx">fileSizes</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}()</span><span class="w"></span>

<span class="w">    </span><span class="c1">// 定期输出结果</span><span class="w"></span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="nx">tick</span><span class="w"> </span><span class="o">&lt;-</span><span class="kd">chan</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Time</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="o">*</span><span class="nx">verbose</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">tick</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Tick</span><span class="p">(</span><span class="mi">500</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Millisecond</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="w"> </span><span class="kt">int64</span><span class="w"></span>
<span class="nx">loop</span><span class="p">:</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">select</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="nx">size</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">fileSizes</span><span class="p">:</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="k">break</span><span class="w"> </span><span class="nx">loop</span><span class="w"> </span><span class="c1">// fileSizes 关闭，则退出，相当于原来的遍历结束</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="nx">nfiles</span><span class="o">++</span><span class="w"></span>
<span class="w">            </span><span class="nx">nbytes</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nx">size</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">tick</span><span class="p">:</span><span class="w"></span>
<span class="w">            </span><span class="nx">printDiskUsage</span><span class="p">(</span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="nx">printDiskUsage</span><span class="p">(</span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>因为这个版本有两个通道需要接收 size、tick，所以无法使用 range 循环了。所以第一个 select 的分支需要通过第二个参数 ok  来判断通道是否已经关闭。这个的 break 退出使用了标签，因为没有标签的 break 只能跳出当前的 select 这层，而这里是需要跳出外层的 for 循环。  </p>
<p>这里的 flag 的解析也值得借鉴，非常简单。首先是解析指定的参数，这里是 -v 参数。多余的参数会通过 flag.Args() 返回一个字符串切片。调用的时候，必须把解析的参数放在前面：</p>
<div class="highlight"><pre><span></span><code><span class="n">PS</span><span class="w"> </span><span class="n">H</span><span class="p">:</span>\<span class="n">Go</span>\<span class="n">src</span>\<span class="n">gopl</span>\<span class="n">ch8</span>\<span class="n">du2</span><span class="o">&gt;</span><span class="w"> </span><span class="n">go</span><span class="w"> </span><span class="n">run</span><span class="w"> </span><span class="n">main</span><span class="o">.</span><span class="n">go</span><span class="w"> </span><span class="o">-</span><span class="n">v</span><span class="w"> </span><span class="n">E</span><span class="p">:</span>\<span class="n">BaiduNetdiskDownload</span><span class="w"> </span><span class="n">E</span><span class="p">:</span>\<span class="n">XMPCache</span><span class="w"> </span><span class="n">E</span><span class="p">:</span>\<span class="n">Downloads</span><span class="w"></span>
<span class="mi">4</span><span class="w"> </span><span class="n">files</span><span class="w">   </span><span class="mf">0.02</span><span class="w"> </span><span class="n">GB</span><span class="w"></span>
<span class="mi">41</span><span class="w"> </span><span class="n">files</span><span class="w">   </span><span class="mf">2.16</span><span class="w"> </span><span class="n">GB</span><span class="w"></span>
<span class="mi">177</span><span class="w"> </span><span class="n">files</span><span class="w">   </span><span class="mf">6.99</span><span class="w"> </span><span class="n">GB</span><span class="w"></span>
<span class="mi">567</span><span class="w"> </span><span class="n">files</span><span class="w">   </span><span class="mf">46.66</span><span class="w"> </span><span class="n">GB</span><span class="w"></span>
<span class="mi">605</span><span class="w"> </span><span class="n">files</span><span class="w">   </span><span class="mf">50.26</span><span class="w"> </span><span class="n">GB</span><span class="w"></span>
<span class="n">PS</span><span class="w"> </span><span class="n">H</span><span class="p">:</span>\<span class="n">Go</span>\<span class="n">src</span>\<span class="n">gopl</span>\<span class="n">ch8</span>\<span class="n">du2</span><span class="o">&gt;</span><span class="w"></span>
</code></pre></div>

<h4 id="ti-gao-bing-fa-xiao-lv"><a class="toclink" href="#ti-gao-bing-fa-xiao-lv">提高并发效率</a></h4>
<p>还可以进一步提高效率，这里的 walkDir 也是可以并发调用从而充分利用磁盘系统的并行机制。这个版本使用了 sycn.WaitGroup 来为并发调用的 walkDir 计数。<br>
当计数器为减为 0 的时候，关闭 fileSizes 通道：</p>
<div class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">verbose</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">flag</span><span class="p">.</span><span class="nx">Bool</span><span class="p">(</span><span class="s">&quot;v&quot;</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;周期性的输出进度&quot;</span><span class="p">)</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 确定初始目录，没变化</span><span class="w"></span>
<span class="w">    </span><span class="nx">flag</span><span class="p">.</span><span class="nx">Parse</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="nx">roots</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">flag</span><span class="p">.</span><span class="nx">Args</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">roots</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">roots</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[]</span><span class="kt">string</span><span class="p">{</span><span class="s">&quot;.&quot;</span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="c1">// 并行遍历每一个文档树</span><span class="w"></span>
<span class="w">    </span><span class="nx">fileSizes</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kt">int64</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="nx">sync</span><span class="p">.</span><span class="nx">WaitGroup</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">root</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">roots</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">n</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">go</span><span class="w"> </span><span class="nx">walkDir</span><span class="p">(</span><span class="nx">root</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="nx">n</span><span class="p">,</span><span class="w"> </span><span class="nx">fileSizes</span><span class="p">)</span><span class="w"> </span><span class="c1">// 注意，多传了一个参数</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">go</span><span class="w"> </span><span class="kd">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">n</span><span class="p">.</span><span class="nx">Wait</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="nb">close</span><span class="p">(</span><span class="nx">fileSizes</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}()</span><span class="w"></span>

<span class="w">    </span><span class="c1">// 定期输出结果，没变化</span><span class="w"></span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="nx">tick</span><span class="w"> </span><span class="o">&lt;-</span><span class="kd">chan</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Time</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="o">*</span><span class="nx">verbose</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">tick</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Tick</span><span class="p">(</span><span class="mi">500</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Millisecond</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="w"> </span><span class="kt">int64</span><span class="w"></span>
<span class="nx">loop</span><span class="p">:</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">select</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="nx">size</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">fileSizes</span><span class="p">:</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="k">break</span><span class="w"> </span><span class="nx">loop</span><span class="w"> </span><span class="c1">// fileSizes 关闭，则退出，相当于原来的遍历结束</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="nx">nfiles</span><span class="o">++</span><span class="w"></span>
<span class="w">            </span><span class="nx">nbytes</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nx">size</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">tick</span><span class="p">:</span><span class="w"></span>
<span class="w">            </span><span class="nx">printDiskUsage</span><span class="p">(</span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="nx">printDiskUsage</span><span class="p">(</span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">printDiskUsage</span><span class="p">(</span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="w"> </span><span class="kt">int64</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;%d files   %.2f GB\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nb">float64</span><span class="p">(</span><span class="nx">nbytes</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">30</span><span class="p">))</span><span class="w"> </span><span class="c1">// 1&lt;&lt;30 就是 2**30 就是 1024*1024*1024</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">walkDir</span><span class="p">(</span><span class="nx">dir</span><span class="w"> </span><span class="kt">string</span><span class="p">,</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">*</span><span class="nx">sync</span><span class="p">.</span><span class="nx">WaitGroup</span><span class="p">,</span><span class="w"> </span><span class="nx">fileSizes</span><span class="w"> </span><span class="kd">chan</span><span class="o">&lt;-</span><span class="w"> </span><span class="kt">int64</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// 注意，多了个参数</span><span class="w"></span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="nx">n</span><span class="p">.</span><span class="nx">Done</span><span class="p">()</span><span class="w"> </span><span class="c1">// 记得退出时计数器要减1</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">entry</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">dirents</span><span class="p">(</span><span class="nx">dir</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">IsDir</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">subdir</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">filepath</span><span class="p">.</span><span class="nx">Join</span><span class="p">(</span><span class="nx">dir</span><span class="p">,</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">Name</span><span class="p">())</span><span class="w"></span>
<span class="w">            </span><span class="nx">walkDir</span><span class="p">(</span><span class="nx">subdir</span><span class="p">,</span><span class="w"> </span><span class="nx">n</span><span class="p">,</span><span class="w"> </span><span class="nx">fileSizes</span><span class="p">)</span><span class="w"> </span><span class="c1">// 注意，多了个参数</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">fileSizes</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">Size</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<h4 id="xian-zhi-bing-fa"><a class="toclink" href="#xian-zhi-bing-fa">限制并发</a></h4>
<p>还需要限制一下并发数，这里要修改一下 dirents 函数来使用计数信号量进行限制，防止同时打开太多的文件：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// 用于限制目录并发数的计数信号量</span><span class="w"></span>
<span class="kd">var</span><span class="w"> </span><span class="nx">sema</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kd">struct</span><span class="p">{},</span><span class="w"> </span><span class="mi">20</span><span class="p">)</span><span class="w"></span>

<span class="c1">// dirents 返回 dir 目录中的条目</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="nx">dirents</span><span class="p">(</span><span class="nx">dir</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">[]</span><span class="nx">os</span><span class="p">.</span><span class="nx">FileInfo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">sema</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="kd">struct</span><span class="p">{}{}</span><span class="w">        </span><span class="c1">// 获取令牌</span><span class="w"></span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="kd">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">sema</span><span class="w"> </span><span class="p">}()</span><span class="w"> </span><span class="c1">// 释放令牌</span><span class="w"></span>
<span class="w">    </span><span class="nx">entries</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">ioutil</span><span class="p">.</span><span class="nx">ReadDir</span><span class="p">(</span><span class="nx">dir</span><span class="p">)</span><span class="w"> </span><span class="c1">// 这个打开文件的操作需要限制并发，在这句之前加上计数信号量，非常合适</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Fprintf</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stderr</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;du1: %v\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">entries</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>现在这个版本的是最好的了。不过下面还会再增加一个取消的操作，这里的取消会用到广播的机制。  </p>
<h3 id="qu-xiao-guang-bo"><a class="toclink" href="#qu-xiao-guang-bo">取消（广播）</a></h3>
<p>一个 goroutine 无法直接终止另一个，因为这样会让所有的共享变量状态处于不确定状态。正确的做法是使用通道来传递一个信号，当 goroutine 接收到信号时，就终止自己。这里要讨论的是如何同时取消多个 goroutine。<br>
一个可选的做法是，给通道发送你要取消的 goroutine 同样多的信号。但是如果一些 goroutine 已经自己终止了，这样计数就多了，就会在发送过程中卡住。如果某些 goroutine 还会自我繁殖，那么信号的数量又会太少。通常，任何时刻都很难知道有多少个 goroutine 正在工作。对于取消操作，这里需要一个可靠的机制在一个通道上<strong>广播</strong>一个事件，这样所有的 goroutine 就都能收到信号，而不用关心具体有多少个 goroutine。<br>
当一个通道关闭且已经取完所有发送的值后，接下来的接收操作都会立刻返回，得到零值。就可以利用这个特性来创建一个广播机制。第一步，创建一个取消通道，在它上面不发送任何的值，但是它的关闭表明程序需要停止它正在做的事情。<br>
这节，先讲解取消广播的实现。然后把这个功能加到上面的例子中去。  </p>
<h4 id="cha-xun-zhuang-tai"><a class="toclink" href="#cha-xun-zhuang-tai">查询状态</a></h4>
<p>还要定义一个工具函数 cancelled，在它被调用的时候检测或<strong>轮询</strong>取消状态：</p>
<div class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">done</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kd">struct</span><span class="p">{})</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">cancelled</span><span class="p">()</span><span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">select</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">done</span><span class="p">:</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">true</span><span class="w"></span>
<span class="w">    </span><span class="k">default</span><span class="p">:</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<h4 id="fa-song-qu-xiao-guang-bo"><a class="toclink" href="#fa-song-qu-xiao-guang-bo">发送取消广播</a></h4>
<p>接下来，创建一个读取标准输入的 goroutine，它通常连接到终端，当用户按回车后，这个 goroutine 通过关闭 done 通道来广播取消事件：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// 当检测到输入时，广播取消</span><span class="w"></span>
<span class="k">go</span><span class="w"> </span><span class="kd">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">os</span><span class="p">.</span><span class="nx">Stdin</span><span class="p">.</span><span class="nx">Read</span><span class="p">(</span><span class="nb">make</span><span class="p">([]</span><span class="kt">byte</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">))</span><span class="w"> </span><span class="c1">// 读一个字节</span><span class="w"></span>
<span class="w">    </span><span class="nb">close</span><span class="p">(</span><span class="nx">done</span><span class="p">)</span><span class="w"></span>
<span class="p">}()</span><span class="w"></span>
</code></pre></div>

<h4 id="xiang-ying-qu-xiao-cao-zuo"><a class="toclink" href="#xiang-ying-qu-xiao-cao-zuo">响应取消操作</a></h4>
<p>现在要让所有的 goroutine 来响应这个取消操作。在主 goroutine 中的 select 中，尝试从 done 接收。如果接收到了，就需要进行取消操作，但是在结束之前，它必须耗尽 fileSizes 通道，丢弃它所有的值，直到通道关闭。<br>
这么做是为了保证所有的 walkDir 调用可以执行完，不会卡在向 fileSizes 通道发送消息上：</p>
<div class="highlight"><pre><span></span><code><span class="k">for</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">select</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">done</span><span class="p">:</span><span class="w"></span>
<span class="w">        </span><span class="c1">// 耗尽 fileSizes，让已经创建的 goroutine 结束</span><span class="w"></span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">fileSizes</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="c1">// 什么也不做</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"></span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="nx">siez</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">fileSizes</span><span class="p">:</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">break</span><span class="w"> </span><span class="nx">loop</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="nx">nfiles</span><span class="o">++</span><span class="w"></span>
<span class="w">        </span><span class="nx">nbytes</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nx">siez</span><span class="w"></span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">tick</span><span class="p">:</span><span class="w"></span>
<span class="w">        </span><span class="nx">printDiskUsage</span><span class="p">(</span><span class="nx">nfiles</span><span class="p">,</span><span class="w"> </span><span class="nx">nbytes</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>walkDir 的 goroutine 在开始的时候轮询取消状态。<br>
如果是取消的状态，就什么都不做立即返回。这样在取消后创建的 goroutine 就会什么都不做而是立刻返回：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">walkDir</span><span class="p">(</span><span class="nx">dir</span><span class="w"> </span><span class="kt">string</span><span class="p">,</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">*</span><span class="nx">sync</span><span class="p">.</span><span class="nx">WaitGroup</span><span class="p">,</span><span class="w"> </span><span class="nx">fileSizes</span><span class="w"> </span><span class="kd">chan</span><span class="o">&lt;-</span><span class="w"> </span><span class="kt">int64</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="nx">n</span><span class="p">.</span><span class="nx">Done</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">cancelled</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">entry</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">dirents</span><span class="p">(</span><span class="nx">dir</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">IsDir</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">subdir</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">filepath</span><span class="p">.</span><span class="nx">Join</span><span class="p">(</span><span class="nx">dir</span><span class="p">,</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">Name</span><span class="p">())</span><span class="w"></span>
<span class="w">            </span><span class="nx">walkDir</span><span class="p">(</span><span class="nx">subdir</span><span class="p">,</span><span class="w"> </span><span class="nx">n</span><span class="p">,</span><span class="w"> </span><span class="nx">fileSizes</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">fileSizes</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">Size</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>现在基本就避免了在取消后创建新的 goroutine。但是其他已经创建的 goroutine 则会等待他们执行完毕。要想更快的响应，就需要对程序逻辑进行侵入式的修改。要确保在取消事件之后没有更多昂贵的操作发生。这就需要更新更多的代码，但是通常可以通过在少量重要的地方检察取消状态来达到目的。<br>
在 dirents 中获取信号量令牌的操作也可需要快速结束：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">dirents</span><span class="p">(</span><span class="nx">dir</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">[]</span><span class="nx">os</span><span class="p">.</span><span class="nx">FileInfo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">select</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="nx">sema</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="kd">struct</span><span class="p">{}{}:</span><span class="w"> </span><span class="c1">// 获取令牌</span><span class="w"></span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">done</span><span class="p">:</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="c1">// 取消</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="kd">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">sema</span><span class="w"> </span><span class="p">}()</span><span class="w"> </span><span class="c1">// 释放令牌</span><span class="w"></span>
<span class="w">    </span><span class="nx">entries</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">ioutil</span><span class="p">.</span><span class="nx">ReadDir</span><span class="p">(</span><span class="nx">dir</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Fprintf</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stderr</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;du1: %v\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">entries</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>现在，当取消事件发生时，已经进入 dirents 函数的调用，如果已经获取到了令牌，则会执行完毕，但是返回后，在地递归调用 walkDir 的时候就会快速退出。那些还没获取令牌的调用，此时在 select 中会因为从 done 通道中接收到取消的广播而直接返回 nil。  </p>
<h4 id="ce-shi-de-ji-qiao"><a class="toclink" href="#ce-shi-de-ji-qiao">测试的技巧</a></h4>
<p>期望的情况是，当然是当取消事件到来时 main 函数可以返回，然后程序随之退出。如果发现在取消事件到来的时候 main 函数没有返回，可以执行一个 panic 调用。从崩溃的转存储信息中通常含有足够的信息来帮助我们分析，发现哪些 goroutine 还没有合适的取消。也可能是已经取消了，但是需要的时间比较长。总之，使用 panic 可以帮助查找原因。  </p>
      </div>
      <div class="back-to-top">
        <a href="/">HOME</a>
        <a href="#top">TOP</a>
      </div>
    </article>
  </div>
<!-- end article -->
<!-- 页面往下滚动一段之后才会显示TOC -->
<script>
  window.onscroll = function() {
    var tocbox = document.getElementsByClassName('toc')[0];
    var osTop = document.documentElement.scrollTop || document.body.scrollTop;
    var osWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
    // console.log(osTop)
    if (osTop>300 && osWidth>865) {
      tocbox.style.display = "block"
    }
    if (osTop<300 || osWidth<865) {
      tocbox.style.display = "none"
    }
  }
</script>
                <footer>
                    <div class="icons">
                    </div>
                    <span id="busuanzi_container_page_pv" style="padding: 10px">本文阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                    <span id="busuanzi_container_site_pv" style="padding: 10px">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
                    <span id="busuanzi_container_site_uv" style="padding: 10px">本站总访客数<span id="busuanzi_value_site_uv"></span>人</span>
                    <p>© <script>document.write(moment().format('YYYY'));</script> 749B</p>
                </footer>
        </div>
</body>
</html>